﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Comm.DBHelper;
using System.Reflection;
using Comm.PublicClass;
using System.Windows.Forms;
using Comm.MultiLan;

namespace WsProtocol.IEC62056MDL
{
    [Serializable]
    public class HHUUser
    {
        private static string ptltable = "T_HHUUser";
        public static List<HHUUser> AllHHUUserlst = new List<HHUUser>();  //所有HHU用户列表

        public string ID { get; set; }  //ID
        public string HuuName { get; set; } //用户名
        public string HuuPsw { get; set; }   //密码


        public HHUUser()
        {
            HuuName = "";
            HuuPsw = "";
        }

        public static void StartTrans()
        {
            PtlConsts.easyDb.BeginTrans();
        }

        public static void RollbackTrans()
        {
            PtlConsts.easyDb.RollbackTrans();
        }

        public static void CommitTrans()
        {
            PtlConsts.easyDb.CommitTrans();
        }


        private static List<HHUUser> TableToList(DataTable dt)
        {
            List<HHUUser> list = new List<HHUUser>();
            foreach (DataRow dr in dt.Rows)
            {
                HHUUser m = GetModelByDataRow(dr);
                
                list.Add(m);
            }
            return list;
        }


        /// <summary>
        /// 数据行转对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static HHUUser GetModelByDataRow(DataRow dr)
        {
            HHUUser m = new HHUUser();
            m.ID = dr["ID"].ToString();
            m.HuuName = dr["HuuName"].ToString();
            m.HuuPsw = dr["HuuPsw"].ToString();
            
            return m;
        }
       

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public static List<HHUUser> GetAllHHUUser()
        {            
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.SetOrder("HuuName");
            AllHHUUserlst = TableToList(PtlConsts.easyDb.Select());
            return AllHHUUserlst;
        }



        /// <summary>
        /// 保存用户信息到数据库 
        /// </summary>
        /// <param name="errinfo"></param>
        /// <returns></returns>
        public int SaveHHUUser(ref string errinfo)
        {
            int rs = 0;
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.SetFieldValue("HuuName", HuuName, EasyDataType.String);
            PtlConsts.easyDb.SetFieldValue("HuuPsw", HuuPsw, EasyDataType.String);

            if (string.IsNullOrEmpty(ID))  //插入
            {
                ID = Guid.NewGuid().ToString();
                PtlConsts.easyDb.SetFieldValue("ID", ID, EasyDataType.String);
                rs = PtlConsts.easyDb.Insert(ref errinfo);
                if (rs > 0)
                    AllHHUUserlst.Add(this);
            }
            else   //修改
            {
                PtlConsts.easyDb.SetWhereParameter(true, "ID", "=", ID, EasyDataType.String);
                rs = PtlConsts.easyDb.Update(ref errinfo);
            }
            if (rs <= 0)
                rs = -1;
            
            return rs;
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="errinfo"></param>
        /// <returns></returns>
        public static int DeleteHHUUser(string id, ref string errinfo)
        {
            if (string.IsNullOrEmpty(id)) return 1;
            try
            {
                PtlConsts.easyDb.Clear();
                PtlConsts.easyDb.TableName = ptltable;
                PtlConsts.easyDb.SetWhereParameter(true, "ID", "=", id, EasyDataType.String);
                int rs = PtlConsts.easyDb.Delete(ref errinfo);
                
                return rs;
            }
            catch(Exception ex)
            {
                errinfo = ex.Message;
                return -1;
            }
        }


    }
}
